#include "obj_window.h"

int obj_window::load(const char * filename){
	debug("");
	FILE * _file;
	_file = fopen(filename,"r");
	if(_file==NULL){
		debug("File does not exist");
		return -1;
	}
	
	if(fscanf(_file,"%f", &x)==EOF){debug("Unexpected End Of File");return -1;}
	if(fscanf(_file,"%f", &y)==EOF){debug("Unexpected End Of File");return -1;}
	if(fscanf(_file,"%f", &w)==EOF){debug("Unexpected End Of File");return -1;}
	if(fscanf(_file,"%f", &h)==EOF){debug("Unexpected End Of File");return -1;}
	if(fscanf(_file,"%d", &textures_num)==EOF){debug("Unexpected End Of File");return -1;}
	if(fscanf(_file,"%d", &children_num)==EOF){debug("Unexpected End Of File");return -1;}
	z=-0.001;
	parent=NULL;

	verts.resize(4);
	faces.resize(2);
	UVs.resize(4);
	textures.resize(textures_num);
	children.resize(children_num);
	for (int i=0;i<children_num;i++)
		children[i]=new obj_window;
	
	verts[0][0]=0;verts[0][1]=0;verts[0][2]=0;
	verts[1][0]=w;verts[1][1]=0;verts[1][2]=0;
	verts[2][0]=w;verts[2][1]=h;verts[2][2]=0;
	verts[3][0]=0;verts[3][1]=h;verts[3][2]=0;

	faces[0][0]=0;faces[0][1]=1;faces[0][2]=2;faces[0][3]=0;faces[0][4]=1;faces[0][5]=2;
	faces[1][0]=0;faces[1][1]=2;faces[1][2]=3;faces[1][3]=0;faces[1][4]=2;faces[1][5]=3;

	UVs[0][0]=0;UVs[0][1]=0;
	UVs[1][0]=1;UVs[1][1]=0;
	UVs[2][0]=1;UVs[2][1]=1;
	UVs[3][0]=0;UVs[3][1]=1;
	
	char _filename[strlen(filename)+8];
	for (int i=0;i<textures_num;i++){
		sprintf(_filename,"%s%d.tga",filename,i);
		textures[i]=load_tga_texture(_filename);
		if (textures[i]==(GLuint)-1){
			sprintf(_filename,"%s%d.jpg",filename,i);
			textures[i]=load_jpg_texture(_filename);
			if (textures[i]==(GLuint)-1){
				debug("Can't find right picture");
				return -1;
			}
		}
		else
			transparent=1;

	}

	for (int i=0;i<children_num;i++){
		if(fscanf(_file,"%s", _filename)==EOF){debug("Unexpected End Of File");return -1;}
		children[i]->load(_filename);
		children[i]->z+=z;
		children[i]->parent=this;
		
	}
	
	loaded=1;
	texture=textures[0];	//TODO temporary line
	clicked=0;
	calc_r();
	transparent=1;
	obj_type=OBJ_TYPE_WINDOW;
	return 0;
}

void obj_window::draw(){
	glPushMatrix();
		glTranslatef(x,y,z);
		draw_obj(this);
	glPopMatrix();

	for (int i=0;i<children_num;i++)
		children[i]->draw();

}
void obj_window::update(){
	if (parent==NULL)
		debug("\n");

	if (kb_mx()>x&&kb_mx()<x+w&&kb_my()>y&&kb_my()<y+h){
		npressed=WIN_OVER;
		if (pressed & WIN_PRESSED){	//naposledy bolo stlacene
			if (kb(KB_M1)==KB_STATUS_PRESSED)	//stale je stlacene
				npressed|=WIN_PRESSED;
			else
				if (kb(KB_M1)==KB_STATUS_CHANGED)	// prave pustil po uspesnom kliknuti
					clicked++;
		}
		else
			if (kb(KB_M1)==(KB_STATUS_PRESSED|KB_STATUS_CHANGED))	//prave uspesne klikol
				npressed|=WIN_PRESSED;
	}
	else
		npressed=0;

	if (npressed!=pressed){
		if (npressed==0&&textures_num>0)
			texture=textures[0];
		if (npressed==WIN_OVER&&textures_num>1)
			texture=textures[1];
		if (npressed==(WIN_OVER|WIN_PRESSED)&&textures_num>2)
			texture=textures[2];
	}
	
	debug("clicked %d\n",clicked);

	pressed=npressed;

	for (int i=0;i<children_num;i++)
		children[i]->update();
	
}
